﻿#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using Flynn.Core;

namespace Flynn
{
    public class Program
    {
        static void Main(string[] args)
        {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();

            Version appVersion = executingAssembly.GetName().Version;
            string applicationTitle = string.Format("Flynn Console Application - Version {0}.{1}.{2} Beta 3", appVersion.Major, appVersion.Minor, appVersion.Build);

            AssemblyCopyrightAttribute copyrightAttribute = (AssemblyCopyrightAttribute)executingAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0];

            Console.Title = applicationTitle;
            Console.WriteLine();
            Console.WriteLine(applicationTitle);
            Console.WriteLine(copyrightAttribute.Copyright);
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine();

            try
            {
                Engine engine = CreateAndSetUpEngine();

                ConsoleRunner consoleRunner = new ConsoleRunner(engine);
                //consoleRunner.Execute(args);
                string output = consoleRunner.Execute(new string[] { "/task:check", "/toversion:3", "/project:FlynnDemo.xml", @"/connection:Data Source=(local);Initial Catalog=FlynnDemo;Integrated Security=True;Pooling=False" });
                Console.WriteLine(output);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Unhandled exception:");
                Console.WriteLine(exception);
            }

            Console.WriteLine();
            Console.WriteLine("Done.");
            Console.ReadLine();
        }

        private static Engine CreateAndSetUpEngine()
        {
            Engine engine = new Engine();

            ScanAssembliesForConnectionFactories(engine);
            ScanAssembliesForVersionings(engine);

            return engine;
        }

        private static void ScanAssembliesForConnectionFactories(Engine engine)
        {
            ConnectionFactoriesCollector connectionFactoriesCollector = new ConnectionFactoriesCollector();
            IDictionary<string, string> connectionFactories = connectionFactoriesCollector.CollectConnectionFactories(AppDomain.CurrentDomain.BaseDirectory);
            foreach (KeyValuePair<string, string> connectionFactory in connectionFactories)
            {
                engine.RegisterDatabase(Type.GetType(connectionFactory.Value), connectionFactory.Key);
            }
        }

        private static void ScanAssembliesForVersionings(Engine engine)
        {
            VersioningsCollector versioningsCollector = new VersioningsCollector();
            IDictionary<string, string> versionings = versioningsCollector.CollectVersionings(AppDomain.CurrentDomain.BaseDirectory);
            foreach (KeyValuePair<string, string> versioning in versionings)
            {
                engine.RegisterVersioning(Type.GetType(versioning.Value), versioning.Key);
            }
        }
    }
}
